package com.junzhiit.nengji.app.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.app.service.SystemTypeService;
import com.junzhiit.nengji.app.web.util.Constants;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.SystemTypeGoodsVO;
import com.junzhiit.nengji.app.web.vo.SystemTypeVO;
import com.junzhiit.nengji.dao.SystemTypeDAO;
import com.junzhiit.nengji.model.SystemType;

@Service
public class SystemTypeServiceImpl extends BaseServiceImpl implements SystemTypeService {

	@Autowired
	private SystemTypeDAO systemTypeDAO;
	
	@Value("${upload.temp}")
	private String uploadPath;

	@Value("${upload.mall.goods.type}")
	private String typePath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;

	@Override
	public ResultVO findAll() {
		ResultVO result = new ResultVO();
		List<SystemType> sourceList = source();
		result.setData(sourceList);
		return result;
	}
	

	@Override
	public ResultVO findAllGrading() {
		ResultVO result = new ResultVO();
		List<SystemType> sourceList = source();
		List<SystemTypeVO> aimsList = new ArrayList<SystemTypeVO>();
		if (!sourceList.isEmpty()) {
			tree(aimsList, sourceList, Constants.ROOT_VALUE);
		}
		result.setData(aimsList);
		return result;
	}

	@Override
	public ResultVO findAllNameGrading() {
		ResultVO result = new ResultVO();
		List<SystemType> sourceList = source();
		List<SystemTypeVO> aimsList = new ArrayList<SystemTypeVO>();
		List<SystemTypeGoodsVO> aimsGoodsList = new ArrayList<SystemTypeGoodsVO>();
		if (!sourceList.isEmpty()) {
			tree(aimsList, sourceList, Constants.ROOT_VALUE);
		}
		if(!aimsList.isEmpty()){
			nameGradingRecursive(aimsGoodsList,aimsList);
		}
		result.setData(aimsGoodsList);
		return result;
	}
	
	private List<SystemType> source(){
		List<SystemType> sourceList = systemTypeDAO.findAll();
		return sourceList;
	}
	
	private void nameGradingRecursive(List<SystemTypeGoodsVO> aimsList,List<SystemTypeVO> sourceList){
		for(SystemTypeVO info:sourceList){
			SystemTypeGoodsVO goodsvo = new SystemTypeGoodsVO();
			BeanUtils.copyProperties(info, goodsvo);
			aimsList.add(goodsvo);
			goodsvo.setChildrenGoods(new ArrayList<SystemTypeGoodsVO>());
			if(info.getChildren() !=null && info.getChildren().size()>0){
				nameGradingRecursive(goodsvo.getChildrenGoods(),info.getChildren());
			}
		}
	}
	
	private void tree(List<SystemTypeVO> aimsList, List<SystemType> sourceList, String parentId) {
		for(int i = 0;i<sourceList.size();i++){
			SystemType type = sourceList.get(i);
			if (parentId.equals(type.getId())) {
				SystemTypeVO typeVO = new SystemTypeVO();
				BeanUtils.copyProperties(type, typeVO);
				aimsList.add(typeVO);
				sourceList.remove(type);
				typeVO.setChildren(new ArrayList<SystemTypeVO>());
				tree(typeVO.getChildren(), sourceList, typeVO.getParentId());
				break;
			} else if (parentId.equals(type.getParentId())) {
				SystemTypeVO typeVO = new SystemTypeVO();
				BeanUtils.copyProperties(type, typeVO);
				aimsList.add(typeVO);
				sourceList.remove(type);
				i = -1;
				typeVO.setChildren(new ArrayList<SystemTypeVO>());
				tree(typeVO.getChildren(), sourceList, typeVO.getId());
			}
		}
	}
	
	@Override
	public ResultVO findAllName() {
		ResultVO result = new ResultVO();
		List<SystemTypeGoodsVO> aimsList = new ArrayList<SystemTypeGoodsVO>();
		List<SystemType> sourceList = source();
		for(SystemType info:sourceList){
			if(info.getLevel()<Constants.SYSTEM_TYPE_LEVEL){
				SystemTypeGoodsVO goodsvo = new SystemTypeGoodsVO();
				BeanUtils.copyProperties(info, goodsvo);
				aimsList.add(goodsvo);
			}
		}
		result.setData(aimsList);
		sourceList.clear();
		return result;
	}

}
